package com.hnxxxy.common.shiro;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.SimpleSession;
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;
import org.springframework.data.redis.core.RedisTemplate;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @CLASSDESCRIPER:
 * @AUTHOR:suhui
 * @PHONE:13319575368
 * @EMIAL:13319575368@163.COM
 * @DATE: 2020/5/8
 * @PARAMDESCRIPER:
 * @OUTPARAMDESCRIPER:
 */
@Slf4j
@Setter
public class ShrioRedisSesssionDao extends AbstractSessionDAO {
    private RedisTemplate<String,Object> redisTemplate;
    /**
     * @param session: 会话
     * @Description: 创建Session ,当发来首次请求，会创建新的Session，此方法中的参数：session即是。
     *  注意：此时的session有SimpleSessionFactory 创建，且还没有sessionID。
     * @Author: SUHUI
     * @EMAIL:13319575368@163.COM
     * @Date: 2020/5/8 16:26
     * @ACTION:
     * @return: java.io.Serializable
     **/
    @Override
    protected Serializable doCreate(Session session) {
        Serializable Sessionid=this.generateSessionId(session);
       log.info("-->创建Session:"+Sessionid);
        this.assignSessionId(session,Sessionid);
        redisTemplate.opsForValue().set("ShrioSession:"+Sessionid,session,1800, TimeUnit.SECONDS);
        return Sessionid;
    }

    /**
     * @Description: 获取Session
     * @Author: SUHUI
     * @EMAIL:13319575368@163.COM
     * @Date: 2020/5/8 16:28
     * @ACTION:
     * @param serializable:
     * @return: org.apache.shiro.session.Session
     **/
    @Override
    protected Session doReadSession(Serializable serializable) {
        //System.out.println("读Session:"+serializable);
        SimpleSession session = (SimpleSession) redisTemplate.opsForValue().get("ShrioSession:" + serializable);
        return session;
    }

    /**
     * @Description: 更新session
     * @Author: SUHUI
     * @EMAIL:13319575368@163.COM
     * @Date: 2020/5/8 16:28
     * @ACTION:
     * @param session:
     * @return: void
     **/
    @Override
    public void update(Session session) throws UnknownSessionException {
        //System.out.println("更新session:"+session.getId());
        redisTemplate.opsForValue().set("ShrioSession:"+session.getId(),session,1800, TimeUnit.SECONDS);
    }

    /**
     * @Description:删除session
     * @Author: SUHUI
     * @EMAIL:13319575368@163.COM
     * @Date: 2020/5/8 16:28
     * @ACTION:
     * @param session:
     * @return: void
     **/
    @Override
    public void delete(Session session) {
       log.info("-->过期session"+session.getId());
        redisTemplate.delete("ShrioSession:"+session.getId());
    }

    /**
     * @Description: 收集所有的session用来做决策
     * @Author: SUHUI
     * @EMAIL:13319575368@163.COM
     * @Date: 2020/5/8 16:29
     * @ACTION:
     * @return: java.util.Collection<org.apache.shiro.session.Session>
     **/
    @Override
    public Collection<Session> getActiveSessions() {
        Set keys = redisTemplate.keys("ShrioSession:*");
        List<Session> sessionList = redisTemplate.opsForValue().multiGet(keys);
        return sessionList;
    }
}
